package org.neuedu.service;

import org.neuedu.bean.Article;
import org.neuedu.dao.*;
import org.neuedu.bean.*;
import org.neuedu.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class ArticleServiceImpl implements ArticleService {

    ArticleDao articleDao = new ArticleDaoImpl();
    @Override
    public int saveArticleInfo(Article article) {
        int i = articleDao.saveArticleInfo(article);

        return i;
    }

    @Override
    public User updateKissnum(Integer kiss, Integer id) {
        UserDao userDao = new UserDaoImpl();
        return userDao.updateKissnum(kiss,id);
    }
    CategoryDao categoryDao = new CategoryDaoImpl();
    @Override
    public IndexLoader loadIndexInfo(Integer tid) {
        // 获取文章分类信息
        List<Category> categoryList = categoryDao.serchAllCateListByRole();
        // 获取置顶信息
        List<Article> topArticleList = articleDao.getTopArticleLiset();
        // 获取主要文章信息
        List<Article> tenMainArticleList = articleDao.getTenMainArticleList(tid);
        List<Article> leftartilelist= articleDao.getTopArticleLiset1();
        List<Article> articleList = articleDao.getbottomArticleLiset();
        // 获取回答周榜
        // 获取本周热议
        IndexLoader indexLoader = new IndexLoader();
        indexLoader.setTopArticleList(topArticleList);
        indexLoader.setMainArticleList(tenMainArticleList);
        indexLoader.setCategoryList(categoryList);
        indexLoader.setLiftArticleList(leftartilelist);
        indexLoader.setBottomArticleList(articleList);

        return indexLoader;
    }

    @Override
    public Detail getArticleDetailById(Integer id) {
        Detail detail = new Detail();
        // 查询文章详情+作者信息
        Article article = articleDao.getArticleById(id);
        // 查询文章评论信息
        List<Reply> reply = articleDao.getReplyByAid(id);
        article.setReplyList(reply);
        detail.setArticle(article);
        // 查询本周热议，存入detail -- 自己完成
        articleDao.updateArticleViewsById(id);
        return detail;
    }

    @Override
    public IndexLoader getArticleInfo(Integer cid) {
        IndexLoader indexLoader = new IndexLoader();
        List<Article> dhInfo = articleDao.getDhInfo(cid);
        indexLoader.setDhArticleList(dhInfo);
        return indexLoader;
    }

        @Override
        public int serchArticleCountByid(Integer id) {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            int count = 0;
            try {
                conn = DBUtils.getInstance().getConnection();
                String sql = "select count(*) count from article where uid = ?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1,id);
                rs = ps.executeQuery();
                if(rs.next()){
                    count = rs.getInt("count");
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                DBUtils.getInstance().close(rs);
                DBUtils.getInstance().close(ps);
                DBUtils.getInstance().close(conn);
            }
            return count;
        }
    }


